package qnb._power;
//package test.power.config;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import qnb._config.SpringContextUtil;
import qnb.entity.Student;
import qnb.entity.SysPower;
import qnb.entity.Teacher;
import qnb.service.StudentService;
import qnb.service.SysPowerService;
import qnb.service.TeacherService;
import qnb.service.impl.SysPowerServiceImpl;
import weixinkeji.vip.jweb.power.config.JWPUserInterface;
import weixinkeji.vip.jweb.power.vo.JWPCodeVO;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户端请求的url 触发权限检验时，权限框架会调用此接口的实例对象的方法（主要是为了拿到用户的权限
 * SessionCodeAndIdentifiterCode）
 *
 * @author wangchunzi
 */
public class UserPowerCode implements JWPUserInterface {
    private static SysPowerService sysPowerService;


    /**
     * 获取【会员等级】、【权限编号】的用户接口
     *
     * @param req  基于Servlet的HttpServletRequest
     * @param resp 基于Servlet的HttpServletResponse
     * @return SessionCodeAndIdentifiterCodeVO 存储临时 值。返回多值* 封装
     */
    @Override
    public JWPCodeVO getUserPowerCode(HttpServletRequest req, HttpServletResponse resp) {
        JWPCodeVO vo = new JWPCodeVO();
        try {
            if (sysPowerService==null){
                sysPowerService = (SysPowerService) SpringContextUtil.getBean("sysPowerService");
            }
            List<String> codes = sysPowerService.getCodes(req.getSession());
            if (!CollectionUtils.isEmpty(codes)) {
                vo.setIdentifiter(codes.toArray(new String[codes.size()]));
            }
        } catch (Exception e) {
        }
        return vo;
    }

}
